#!/bin/bash
# Check for dependencise
#依赖库检查

# Link colour table
source /home/pi/scripts/colourtable.sh

# Link config file
source /home/pi/scripts/picraft.conf

# Get time via founction
Rd() {
  date +"%Y-%m-%d %H:%M:%S"
}


echo "Dependency Check"
echo "[$(Rd)] Dependency check script started. This usually runs ones, and skipped in the future start up." >> /home/pi/scripts/log.txt
sleep 1

# Check for expect
if command -v expect >/dev/null 2>&1 ;
then
    echo ""
    echo -e "===${ORANGE}Dpc Checker${NC}====================================================="
    echo ""
    echo "expect is found ins your system!"
    echo "version: $(expect -v)"
    echo ""
    echo "==================================================================="
    sleep 1
else
    echo ""
    echo -e "===${RED}Dpc Checker${NC}====================================================="
    echo ""
    echo "expect is not found in your system!"
    echo ""
    echo "==================================================================="
    # Log event
    echo "[$(Rd)] expect is not found in the system." >> /home/pi/scripts/log.txt
    # End Log event 
    sleep 1
    echo "Trying to install for you..."
    # Log event
    echo "[$(Rd)] Trying to install expect" >> /home/pi/scripts/log.txt
    # End Log event
    echo ""
    yes Y | apt-get install expect
    sleep 1
    if command -v expect >/dev/null 2>&1 ; then
        echo ""
        echo -e "===${GREEN}Dpc Checker${NC}====================================================="
        echo ""
        echo "Installed!"
        echo "vsersion: $(expect -v)"
        echo ""
        echo "==================================================================="
        echo ""
        # Log event
        echo "[$(Rd)] expect is installed Version: $(expect -v)" >> /home/pi/scripts/log.txt
        # End Log event
        sleep 1
    else
        echo ""
        echo -e "===${RED}Dpc Checker${NC}====================================================="
        echo ""
        echo "Fail to install, please install manually!"
        echo "Try to run bash with sudo?"
        echo "Manual install via apt-get install expect"
        echo ""
        echo "==================================================================="
        sed -i -e '/^dpc=/s|.*|dpc=false|' /home/pi/scripts/picraft.conf
        # Log event
        echo "[$(Rd)] Cannot install expect." >> /home/pi/scripts/log.txt
        # End Log event
        exit 1
    fi
fi

# Check for sysbench
if command -v sysbench >/dev/null 2>&1 ;
then
    echo ""
    echo -e "===${ORANGE}Dpc Checker${NC}====================================================="
    echo ""
    echo "sysbench is found ins your system!"
    echo "version: $(sysbench --version)"
    echo ""
    echo "==================================================================="
    sleep 1
else
    echo ""
    echo -e "===${RED}Dpc Checker${NC}====================================================="
    echo ""
    echo "sysbench is not found in your system!"
    echo ""
    echo "==================================================================="
    # Log event
    echo "[$(Rd)] sysbench is not found in the system." >> /home/pi/scripts/log.txt
    # End Log event 
    sleep 1
    echo "Trying to install for you..."
    # Log event
    echo "[$(Rd)] Trying to install sysbench" >> /home/pi/scripts/log.txt
    # End Log event
    echo ""
    yes Y | apt-get install sysbench
    sleep 1
    if command -v sysbench >/dev/null 2>&1 ; then
        echo ""
        echo -e "===${GREEN}Dpc Checker${NC}====================================================="
        echo ""
        echo "Installed!"
        echo "vsersion: $(sysbench --version)"
        echo ""
        echo "==================================================================="
        # Log event
        echo "[$(Rd)] sysbench is installed Version: $(sysbench --version)" >> /home/pi/scripts/log.txt
        # End Log event
        sleep 1
    else
        echo ""
        echo -e "===${RED}Dpc Checker${NC}====================================================="
        echo ""
        echo "Fail to install, please install manually!"
        echo "Try to run bash with sudo?"
        echo "Manual install via apt-get install sysbench"
        echo ""
        echo "==================================================================="
        sed -i -e '/^dpc=/s|.*|dpc=false|' /home/pi/scripts/picraft.conf
        # Log event
        echo "[$(Rd)] Fail to install sysbench." >> /home/pi/scripts/log.txt
        # End Log event
        exit 1
    fi
fi

# Check for screen
if command -v screen >/dev/null 2>&1 ;
then
    echo ""
    echo -e "===${ORANGE}Dpc Checker${NC}====================================================="
    echo ""
    echo "screen is found ins your system!"
    echo "version: $(screen -v)"
    echo ""
    echo "==================================================================="
    sleep 1
else
    echo ""
    echo -e "===${RED}Dpc Checker${NC}====================================================="
    echo ""
    echo "screen is not found in your system!"
    echo ""
    echo "==================================================================="
    # Log event
    echo "[$(Rd)] screen is not found in the system." >> /home/pi/scripts/log.txt
    # End Log event 
    sleep 1
    echo "Trying to install for you..."
    # Log event
    echo "[$(Rd)] Trying to install screen" >> /home/pi/scripts/log.txt
    # End Log event
    echo ""
    yes Y | apt-get install screen
    sleep 1
    if command -v screen >/dev/null 2>&1 ; then
        echo ""
        echo -e "===${GREEN}Dpc Checker${NC}====================================================="
        echo ""
        echo "Installed!"
        echo "vsersion: $(screen -v)"
        echo ""
        echo "==================================================================="
        # Log event
        echo "[$(Rd)] screen is installed Version: $(screen -v)" >> /home/pi/scripts/log.txt
        # End Log event
        sleep 1
    else
        echo ""
        echo -e "===${RED}Dpc Checker${NC}====================================================="
        echo ""
        echo "Fail to install, please install manually!"
        echo "Try to run bash with sudo?"
        echo "Manual install via apt-get install screen"
        echo ""
        echo "==================================================================="
        sed -i -e '/^dpc=/s|.*|dpc=false|' /home/pi/scripts/picraft.conf
        echo "[$(Rd)] Fail to install screen." >> /home/pi/scripts/log.txt
        exit 1
    fi
fi

# Check for java
if command -v java >/dev/null 2>&1 ;
then
    echo ""
    echo -e "===${ORANGE}Dpc Checker${NC}====================================================="
    echo ""
    echo "java is found ins your system!"
    echo "version: $(java --version)"
    echo ""
    echo "==================================================================="
    sleep 1
else
    echo ""
    echo -e "===${RED}Dpc Checker${NC}====================================================="
    echo ""
    echo "java is not found in your system!"
    echo ""
    echo "==================================================================="
    # Log event
    echo "[$(Rd)] java(default-jdk) is not found in the system." >> /home/pi/scripts/log.txt
    # End Log event 
    sleep 1
    echo "Trying to install for you..."
    # Log event
    echo "[$(Rd)] Trying to install java(default-jdk)" >> /home/pi/scripts/log.txt
    # End Log event

    echo ""
    yes Y | apt-get install default-jdk
    sleep 1
    if command -v java >/dev/null 2>&1 ; then
    echo ""
        echo -e "===${GREEN}Dpc Checker${NC}====================================================="
        echo ""
        echo "Installed!"
        echo "vsersion: $(java --version)"
        echo ""
        echo "==================================================================="
        # Log event
        echo "[$(Rd)] java(default-jdk) is installed Version: $(java --version)" >> /home/pi/scripts/log.txt
        # End Log event
        sleep 1
    else
        echo ""
        echo -e "===${RED}Dpc Checker${NC}====================================================="
        echo ""
        echo "Fail to install, please install manually!"
        echo "Try to run bash with sudo?"
        echo "Manual install via apt-get install default-jdk"
        echo ""
        echo "==================================================================="
        sed -i -e '/^dpc=/s|.*|dpc=false|' /home/pi/scripts/picraft.conf
        echo "[$(Rd)] Fail to install Java(default-jdk)." >> /home/pi/scripts/log.txt
        exit 1
    fi
fi